/*
 * CatPred.java
 *
 * Created on den 2 maj 2007, 09:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package grex.genes;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import grex.Options;
import grex.Prediction;
import grex.PredictionContainer;

public class CatVal extends Gene {

    private Gene neighbour;
    private static final int NROFCILDREN = 0;
    private double internalValue = -1;
    private double classValue;
    private String realValue = "Not initialized";
    

    public CatVal() {
        super(NROFCILDREN);
        classValue = -1;
        internalValue = Options.rnd.nextDouble();
    }

    private CatVal(double internalValue, double classValue) {
        super(NROFCILDREN);
        this.internalValue = internalValue;
        this.classValue = classValue;
    }

    public synchronized void execute(Gene parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;
        if (mode == Gene.TRAIN_MODE) {
            
            Gene[] childs = parent.getChildren();
            if (childs[0] == this) {
                neighbour = childs[1];
            } else {
                neighbour = childs[0];
            }
            int column = ((CatVar) neighbour).getVarNr();
            classValue = environment.getInternalNumericalValue(column, internalValue,getFold());
            realValue = environment.getRealClassValue(column, internalValue);            
        }

        for (Prediction p : data.values()) {
            p.setPrediction(classValue);
        }
    }

    public void setValue(double classValue) {
        this.classValue = classValue;
    }

    public double getClassValue() {
        return classValue;
    }

    public double getInternalValue() {
        return internalValue;
    }
    
    public void setRealValue(String realValue){
        this.realValue=realValue;
    }

    public Object clone() {
        CatVal newTarget = new CatVal(internalValue, classValue);
        newTarget.setFold(getFold());
        newTarget.setOptions(ops);
        newTarget.setEnvironment(environment);
        newTarget.setRealValue(realValue);
        newTarget.setParent(parent);
        return newTarget;
    }

    /** @todo Om endast enn kategori finns kommer denna att volja lika ofta somma alla andra sammtidigt */
    public String toString() {

        return realValue;
    }
}

